<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户管理 - 电动车销售系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/admin/customerlist.css">
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">客户信息管理</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="d-none d-md-inline">${currentUser.name}</span>
                    <div class="badge bg-light text-dark">
                        <i class="bi bi-person-circle me-1"></i> 系统管理员
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-gear fs-5"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/change_password.jsp"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/LogoutServlet"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="container my-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-0"><i class="bi bi-people me-2"></i>客户管理</h2>
            <p class="text-muted mb-0">管理系统所有客户信息</p>
        </div>
        <div>
            <a href="${pageContext.request.contextPath}/AdminDashboardServlet" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> 返回管理
            </a>
        </div>
        <div>
            <a href="${pageContext.request.contextPath}/addcustomer" class="btn btn-primary">
                <i class="bi bi-person-plus me-1"></i> 添加新客户
            </a>
        </div>
    </div>

    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="summary-card">
                <i class="bi bi-people fs-1 text-primary"></i>
                <div class="number">${totalCount}</div>
                <div class="title">客户总数</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="summary-card">
                <i class="bi bi-telephone fs-1 text-success"></i>
                <div class="number"><fmt:formatNumber value="${totalCount/3}" maxFractionDigits="0"/></div>
                <div class="title">平均联系客户</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="summary-card">
                <i class="bi bi-geo-alt fs-1 text-warning"></i>
                <div class="number">8</div>
                <div class="title">覆盖区域数</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="summary-card">
                <i class="bi bi-currency-exchange fs-1 text-info"></i>
                <div class="number"><fmt:formatNumber value="${totalCount*1465}" pattern="#,###"/></div>
                <div class="title">累计成交订单</div>
            </div>
        </div>
    </div>

    <c:if test="${not empty successMsg}">
        <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-check-circle me-2"></i> ${successMsg}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <c:remove var="successMsg" scope="session"/>
    </c:if>

    <c:if test="${not empty errorMsg}">
        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMsg}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <c:remove var="errorMsg" scope="session"/>
    </c:if>

    <div class="card shadow">
        <div class="card-header d-flex justify-content-between align-items-center py-3">
            <span>所有客户</span>
            <div class="d-flex">
                <form method="get" action="${pageContext.request.contextPath}/CustomerListServlet" class="d-flex">
                    <div class="search-bar me-2">
                        <i class="bi bi-search text-muted me-2"></i>
                        <input type="text" name="keyword" class="border-0 bg-transparent" placeholder="搜索客户..."
                               value="${param.keyword}">
                    </div>
                    <button type="submit" class="btn btn-sm btn-primary me-2">搜索</button>
                    <a href="${pageContext.request.contextPath}/CustomerListServlet" class="btn btn-sm btn-light">
                        <i class="bi bi-arrow-counterclockwise"></i> 重置
                    </a>
                </form>
            </div>
        </div>

        <div class="card-body">
            <c:choose>
                <c:when test="${not empty customers}">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>客户信息</th>
                                <th>联系方式</th>
                                <th>地址</th>
                                <th>注册时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${customers}" var="customer">
                                <tr>
                                    <td><strong>#${customer.id}</strong></td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="me-3">
                                                <div class="customer-avatar">
                                                        ${fn:substring(customer.name, 0, 1)}
                                                </div>
                                            </div>
                                            <div>
                                                <div class="fw-medium">${customer.name}</div>
                                                <div class="text-muted small">客户ID: ${customer.id}</div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>${customer.phone}</td>
                                    <td>${customer.address}</td>
                                    <td>
                                        <fmt:formatDate value="${customer.create_time}" pattern="yyyy-MM-dd HH:mm"/>
                                    </td>
                                    <td>
                                        <div class="d-flex gap-2">
                                            <a href="${pageContext.request.contextPath}/UpdateCustomerServlet?id=${customer.id}"
                                               class="btn btn-sm btn-outline-primary">
                                                <i class="bi bi-pencil"></i> 编辑
                                            </a>
                                            <a href="${pageContext.request.contextPath}/deleteCustomer?id=${customer.id}"
                                               class="btn btn-sm btn-outline-danger delete-btn"
                                               data-customer-name="${customer.name}">
                                                <i class="bi bi-trash"></i> 删除
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页控件 -->
                    <c:if test="${empty param.keyword}">
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <div class="text-muted small">
                                显示 ${(currentPage-1)*10+1} 到
                                    ${currentPage*10 > totalCount ? totalCount : currentPage*10} 项，共 ${totalCount} 项
                            </div>
                            <nav>
                                <ul class="pagination pagination-sm mb-0">
                                    <c:if test="${currentPage > 1}">
                                        <li class="page-item">
                                            <a class="page-link"
                                               href="${pageContext.request.contextPath}/CustomerListServlet?page=${currentPage-1}">
                                                <i class="bi bi-chevron-left"></i> 上一页
                                            </a>
                                        </li>
                                    </c:if>

                                    <c:forEach begin="1" end="${totalPages}" var="pageNum">
                                        <li class="page-item ${pageNum == currentPage ? 'active' : ''}">
                                            <a class="page-link"
                                               href="${pageContext.request.contextPath}/CustomerListServlet?page=${pageNum}">${pageNum}</a>
                                        </li>
                                    </c:forEach>

                                    <c:if test="${currentPage < totalPages}">
                                        <li class="page-item">
                                            <a class="page-link"
                                               href="${pageContext.request.contextPath}/CustomerListServlet?page=${currentPage+1}">
                                                下一页 <i class="bi bi-chevron-right"></i>
                                            </a>
                                        </li>
                                    </c:if>
                                </ul>
                            </nav>
                        </div>
                    </c:if>
                </c:when>
                <c:otherwise>
                    <div class="empty-state">
                        <i class="bi bi-people"></i>
                        <h5>暂无客户数据</h5>
                        <p class="text-muted">系统中还没有任何客户，点击"添加新客户"创建客户</p>
                        <a href="${pageContext.request.contextPath}/addcustomer" class="btn btn-primary">
                            <i class="bi bi-person-plus me-1"></i> 添加新客户
                        </a>
                    </div>
                </c:otherwise>
            </c:choose>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 删除确认
    document.querySelectorAll('.delete-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            const customerName = this.getAttribute('data-customer-name');
            if (!confirm(`确定要删除客户 ${customerName} 吗？此操作不可恢复！`)) {
                e.preventDefault();
            }
        });
    });

    // 自动关闭消息提示
    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(alert => {
            alert.style.display = 'none';
        });
    }, 5000);
</script>
</body>
</html>